package com.astn.bks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.astn.adapter.booking.COfferAdapter;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.DateUtils;
import com.astn.bks.util.JsonUtils;
import com.astn.bks.util.WebserviceUtils;
import com.astn.entity.booking.QuoteOffer;
import com.astn.entity.booking.QuoteRequest;
import com.google.gson.JsonArray;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class COfferOnMyRequest extends Activity {
	private TextView txvAdult;
	private TextView txvChildren;
	private TextView txvFrom;
	private TextView txvTo;
	private TextView txvDestination;
	private ListView lsvOffers;
	private QuoteRequest currentRequest;
	// private ArrayList<QuoteOffer> lstOffer = new ArrayList<QuoteOffer>();
	private JsonArray jarOffer = new JsonArray();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.cofferonrequest);
		currentRequest = (QuoteRequest) getIntent().getSerializableExtra(
				BksConstants.EXTRA_REQUEST);
		txvAdult = (TextView) findViewById(R.id.txvAdult);
		txvChildren = (TextView) findViewById(R.id.txvChildren);
		txvFrom = (TextView) findViewById(R.id.txvFrom);
		txvTo = (TextView) findViewById(R.id.txvTo);
		txvDestination = (TextView) findViewById(R.id.txvDestination);
		lsvOffers = (ListView) findViewById(R.id.lsvOffer);
		if (currentRequest != null) {
			txvAdult.setText("Adult: "
					+ String.valueOf(currentRequest.getAdult()));
			txvChildren.setText("Children: "
					+ String.valueOf(currentRequest.getChildren()));
			txvFrom.setText("From: "
					+ DateUtils.quoteDateFormat.format(currentRequest.getFrom()));
			txvTo.setText("To: "
					+ DateUtils.quoteDateFormat.format(currentRequest.getTo()));
			String destination = "";
			destination += currentRequest.getCityname() != null ? currentRequest
					.getCityname() + "-"
					: "";
			destination += currentRequest.getStatename() != null ? currentRequest
					.getStatename() + "-"
					: "";
			destination += currentRequest.getCountryname();
			txvDestination.setText(destination);
		} else {
			Toast.makeText(this, "Can not load request.", Toast.LENGTH_SHORT)
					.show();
			setResult(BksConstants.RESULT_ERROR);
			finish();
		}
		lsvOffers.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				if (position > 0) {
					Intent i = new Intent(COfferOnMyRequest.this,
							COfferView.class);
					i.putExtra(BksConstants.EXTRA_OFFER,
							jarOffer.get(position - 1).toString());
					startActivityForResult(i, BksConstants.CUSTOMER_OFFER_VIEW);
				}
			}

		});
		jarOffer = new JsonArray();
		try {
			Map<String, Object> mapPara = new HashMap<String, Object>();
			mapPara.put("REQUESTID", currentRequest.getId());
			Object rsObj = WebserviceUtils.call("com_xmlrpc",
					"getOfferOfRequest", mapPara);
			jarOffer = (JsonArray) JsonUtils.parser.parse(rsObj.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		COfferAdapter adap = new COfferAdapter(this, jarOffer);
		lsvOffers.setAdapter(adap);
	}

}
